package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;


/**
 * 移动端预约提交管理
 */

@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;


    @RequestMapping("/addorder")
    public Result addorder(@RequestBody Map map){
        try {
            //判断验证码是否输入正确
            String Code = jedisPool.getResource().get(map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
            System.out.println(Code);
            String validateCode =(String)map.get("validateCode");
            System.out.println(validateCode);
            if (Code==null){
                return new Result(false,"验证码失效,请重新获取");
            }
            if (Code.equals(validateCode)) {
                //调用Service层添加预约
               Result result = orderService.addOrder(map);
               return result;


            }else {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器异常,请稍后再试");
        }

    }


    @RequestMapping("/findById")
    //根据预约ID查询此次预约的部分信息
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }




}
